<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="新机名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineName">
                  <a-input v-model="model.machineName" placeholder="请输入新机名称"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="品牌ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brandId">
                  <j-dict-select-tag type="list" v-model="model.brandId" dictCode="yl_machinery_brand,brand_name,id" placeholder="请选择品牌ID" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="分类ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeId">
                  <j-dict-select-tag type="list" v-model="model.typeId" dictCode="yl_machinery_type,type_name,id" placeholder="请选择分类ID" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineLabel">
                  <a-input v-model="model.machineLabel" placeholder="请输入标签"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="关键字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineWords">
                  <a-input v-model="model.machineWords" placeholder="请输入关键字"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineCover">
                  <j-image-upload isMultiple  v-model="model.machineCover" ></j-image-upload>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="轮播图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineImage">
                  <j-image-upload isMultiple  v-model="model.machineImage" ></j-image-upload>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="轮播视频" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineVideo">
                  <j-upload v-model="model.machineVideo"   ></j-upload>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="分享图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shareImage">
                  <j-image-upload isMultiple  v-model="model.shareImage" ></j-image-upload>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="发动机功率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="transmitterPower">
                  <a-input v-model="model.transmitterPower" placeholder="请输入发动机功率"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="驱动方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="driveType">
                  <a-input v-model="model.driveType" placeholder="请输入驱动方式"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="档位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineGears">
                  <a-input v-model="model.machineGears" placeholder="请输入档位"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="最大牵引力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maxTraction">
                  <a-input v-model="model.maxTraction" placeholder="请输入最大牵引力"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="马力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="horsePower">
                  <a-input v-model="model.horsePower" placeholder="请输入马力"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineSource">
                  <a-input v-model="model.machineSource" placeholder="请输入来源"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineDetail">
                  <j-editor v-model="model.machineDetail" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="是否特价商品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isSpecial">
                  <j-dict-select-tag type="radio" v-model="model.isSpecial" dictCode="yes_no" placeholder="请选择是否特价商品" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="其他参数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineParam">
                  <a-input v-model="model.machineParam" placeholder="请输入其他参数"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineSort">
                  <a-input-number v-model="model.machineSort" placeholder="请输入排序" style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: 'YlNewMachineryModal',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        title:'',
        width:800,
        visible: false,
        disableSubmit: false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           machineName: [
              { required: true, message: '请输入新机名称!'},
           ],
           brandId: [
              { required: true, message: '请输入品牌ID!'},
           ],
           typeId: [
              { required: true, message: '请输入分类ID!'},
           ],
           machineLabel: [
              { required: true, message: '请输入标签!'},
           ],
           machineWords: [
              { required: true, message: '请输入关键字!'},
           ],
           machineCover: [
              { required: true, message: '请输入封面!'},
           ],
           machineImage: [
              { required: true, message: '请输入轮播图!'},
           ],
           shareImage: [
              { required: true, message: '请输入分享图!'},
           ],
           transmitterPower: [
              { required: true, message: '请输入发动机功率!'},
           ],
           driveType: [
              { required: true, message: '请输入驱动方式!'},
           ],
           machineGears: [
              { required: true, message: '请输入档位!'},
           ],
           maxTraction: [
              { required: true, message: '请输入最大牵引力!'},
           ],
           horsePower: [
              { required: true, message: '请输入马力!'},
           ],
           machineSource: [
              { required: true, message: '请输入来源!'},
           ],
           machineDetail: [
              { required: true, message: '请输入详情!'},
           ],
           isSpecial: [
              { required: true, message: '请输入是否特价商品!'},
           ],
           machineParam: [
              { required: true, message: '请输入其他参数!'},
           ],
        },
        url: {
          add: "/new/ylNewMachinery/add",
          edit: "/new/ylNewMachinery/edit",
          queryById: "/new/ylNewMachinery/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.visible=true
        this.$nextTick(()=>{
          this.edit(this.modelDefault);
        })
      },
      edit (record) {
        this.visible=true
        this.$nextTick(()=>{
          this.model = Object.assign({}, record);
          this.visible = true;
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        this.submitForm();
      },
      submitForm () {
          const that = this;
          // 触发表单验证
          this.$refs.form.validate(valid => {
            if (valid) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
              }else{
                httpurl+=this.url.edit;
                 method = 'put';
              }
              httpAction(httpurl,this.model,method).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.submitCallback();
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.confirmLoading = false;
              })
            }

          })
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>
